﻿using Dapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Infrastructrue.Interface
{
    public class BaseRepostity<T> : IBaseRepostity<T> where T : class, new()
    {
        private readonly DbContext db;

        public BaseRepostity(DbContext db)
        {
            this.db = db;
        }

        public async Task<int> DeleteAsync(T entity)
        {
            return await db.SqlSugar.Deleteable(entity).ExecuteCommandAsync();
        }

        public async Task<int> InsertAsync(T entity)
        {
            return await db.SqlSugar.Insertable(entity).ExecuteCommandAsync();
        }

        public async Task<int> UpdateAsync(T entity)
        {
            return await db.SqlSugar.Updateable(entity).ExecuteCommandAsync();
        }
        public async Task<IEnumerable<T>> QueryAsync(string sql, object parameters = null)
        {
            return await db.DapperConnection.QueryAsync<T>(sql, parameters);
        }

        public async Task<T> QueryFirstOrDefaultAsync(string sql, object parameters = null)
        {
            return await db.DapperConnection.QueryFirstOrDefaultAsync<T>(sql, parameters);
        }

        public ISugarQueryable<T> GetAll()
        {
            return db.SqlSugar.Queryable<T>();
        }
    }
}
